package com.tanhua.dubbo.server.api.mongo;


import com.mongodb.client.result.UpdateResult;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;


import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Long save(Comment comment) {
        comment.setId(ObjectId.get());
        comment.setCreated(System.currentTimeMillis());
        this.mongoTemplate.save(comment);
        updateCommentNums(comment,1);
        return findCommentNums(comment);
    }


    public Long remove(Comment comment) {
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);
        updateCommentNums(comment,-1);
        return findCommentNums(comment);
    }

    @Override
    public PageResult findAll(String publishId, Integer page, Integer pageSize) {

        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("created")));

        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(2)).with(pageRequest);

        long count = this.mongoTemplate.count(query, Comment.class);

        List<Comment> commentList = this.mongoTemplate.find(query, Comment.class);

        return new PageResult(page,pageSize,(int)count,commentList);
    }

    private void updateCommentNums(Comment comment,int num) {
        if(comment.getPubType() == 1) {
            Query query1 = new Query().addCriteria(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update().inc(comment.getCol(), num);
            mongoTemplate.upsert(query1,update,Publish.class);
        }else if(comment.getPubType() ==2) {
            Query query1 = new Query().addCriteria(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update().inc(comment.getCol(), num);
            mongoTemplate.upsert(query1,update,Video.class);
        }else {
            Query query1 = new Query().addCriteria(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update().inc(comment.getCol(), num);
            UpdateResult result = mongoTemplate.upsert(query1, update, Comment.class);
        }
    }

    private Long findCommentNums(Comment comment) {
        Criteria criteria1 = Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query1 = Query.query(criteria1);
        return mongoTemplate.count(query1, Comment.class);
    }
}
